<template>
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="80px" @submit.prevent>
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="form.subjectName" placeholder="请输入单行文本学科名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="isFrontDisplay">
          <el-switch v-model="form.isFrontDisplay" active-color="#16EE2F"></el-switch>
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="end">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
</template>
<script>
import { add, update } from '@/api/hmmm/subjects'
import { ElMessage } from 'element-plus'
import { getCurrentInstance, reactive, ref, toRefs } from 'vue'

export default {
  inheritAttrs: false,
  methods: {
    onClose () {
      this.$refs.formRef.resetFields()
    }
  },
  setup (props, { emit }) {
    const data = reactive({
      form: {
        subjectName: undefined,
        isFrontDisplay: true
      },
      rules: {
        subjectName: [{
          required: true,
          message: '请输入单行文本学科名称',
          trigger: 'blur'
        }]
      }
    })
    // const formRef = ref()
    // vue2
    // function handelConfirm () {
    //   formRef.value.validate(async valid => {
    const instance = getCurrentInstance()
    // 提交
    const handelConfirm = () => {
      instance.ctx.$refs.formRef.validate(async valid => {
        if (!valid) return
        if (data.form.id) {
          await update(data.form)
        } else {
          await add(data.form)
        }
        ElMessage.success('操作成功')
        emit('success')
        close()
      })
    }
    // 取消
    const close = () => {
      instance.ctx.$refs.formRef.resetFields()
      emit('close')
    }

    return { handelConfirm, close, ...toRefs(data) }
  }
}

</script>
<style>
</style>
